package com.gemme.gemme.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;

import com.gemme.gemme.R;

/*<crack.video.com.crackvideo.tools.ShapeTextView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginTop="10dp"
        android:text="右上，左下圆角，其它直角"
        android:gravity="center"
        apps:bottomLeftRadius="10dp"
        apps:topRightRadius="10dp"
        apps:strokeWith="5"
        android:textColor="#000000"
        apps:solidColor="#fff"
        apps:strokeColor="#ff0000"
        apps:solidTouchColor="#ff5555"
        apps:strokeTouchColor="#000000"
        apps:textTouchColor="#ffffff"
        apps:shapeTpe="rectangle"
        />
        ————————————————
        版权声明：本文为CSDN博主「猿行天下」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
        原文链接：https://blog.csdn.net/yangyahuiguo/java/article/details/52230397*/

public class ShapeTextView extends androidx.appcompat.widget.AppCompatTextView {


    public ShapeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    //填充色
    private int solidColor = 0;
    //边框色
    private int strokeColor = 0;
    //按下填充色
    private int solidTouchColor = 0;
    //按下边框色
    private int strokeTouchColor = 0;
    //边框宽度
    private int strokeWith = 0;
    private int shapeTpe = 0;
    //按下字体色
    private int textTouchColor=0;
    //字体色
    private int textColor=0;

    //圆角的半径
    float radius =0;
    float topLeftRadius=0;
    float topRightRadius=0;
    float bottomLeftRadius=0;
    float bottomRightRadius=0;


    public void init(Context context, AttributeSet attrs) {
        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ShapeTextView, 0, 0);

        solidColor = ta.getInteger(R.styleable.ShapeTextView_solidColor, 0x00000000);
        strokeColor = ta.getInteger(R.styleable.ShapeTextView_strokeColor, 0x00000000);

        solidTouchColor = ta.getInteger(R.styleable.ShapeTextView_solidTouchColor, 0x00000000);
        strokeTouchColor = ta.getInteger(R.styleable.ShapeTextView_strokeTouchColor, 0x00000000);
        textTouchColor= ta.getInteger(R.styleable.ShapeTextView_textTouchColor, 0x00000000);
        textColor=getCurrentTextColor();
        strokeWith = ta.getInteger(R.styleable.ShapeTextView_strokeWith, 0);

        radius = ta.getDimension(R.styleable.ShapeTextView_radius, 0);
        topLeftRadius = ta.getDimension(R.styleable.ShapeTextView_topLeftRadius, 0);
        topRightRadius = ta.getDimension(R.styleable.ShapeTextView_topRightRadius, 0);
        bottomLeftRadius = ta.getDimension(R.styleable.ShapeTextView_bottomLeftRadius, 0);
        bottomRightRadius = ta.getDimension(R.styleable.ShapeTextView_bottomRightRadius, 0);
        shapeTpe= ta.getInt(R.styleable.ShapeTextView_shapeTpe, GradientDrawable.RECTANGLE);
        ta.recycle();


        if(strokeColor!=0&&strokeWith>0) {
            paintStroke = new Paint();
            paintStroke.setColor(strokeColor);
            paintStroke.setStyle(Paint.Style.STROKE);
            paintStroke.setStrokeWidth(strokeWith);
            paintStroke.setAntiAlias(true);
        }

        paint=new Paint();
        paint.setColor(solidColor);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);

        if(radius==0 && shapeTpe==GradientDrawable.RECTANGLE){
            path=new Path();
            //顺时针绘制 左上 右上 右下 左下
            radii=new float[]{topLeftRadius,topLeftRadius,topRightRadius,topRightRadius,bottomRightRadius,bottomRightRadius,bottomLeftRadius,bottomLeftRadius};
        }
    }

    private final RectF mRect = new RectF();
    //创建一个画笔
    private Paint paintStroke ;
    private Paint paint ;
    private Path path;
    float[] radii;
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mRect.set(strokeWith, strokeWith, getWidth() - strokeWith, getHeight() - strokeWith);
        if(path!=null)
            path.addRoundRect(mRect, radii, Path.Direction.CW);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(shapeTpe== GradientDrawable.RECTANGLE){
            if(radius==0){
                canvas.drawPath(path, paint);
                if(paintStroke!=null)
                    canvas.drawPath(path, paintStroke);
            }else{
                canvas.drawRoundRect(mRect, radius, radius, paint);
                if(paintStroke!=null)
                    canvas.drawRoundRect(mRect, radius, radius, paintStroke);
            }
        }else{
            canvas.drawOval(mRect,paint);
            if(paintStroke!=null)
                canvas.drawOval(mRect,paintStroke);
        }
        super.onDraw(canvas);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (solidTouchColor != 0||paintStroke!=null|| textTouchColor!=0){
            if (event.getAction() == MotionEvent.ACTION_DOWN||event.getAction() == MotionEvent.ACTION_MOVE) {
                if(event.getAction() == MotionEvent.ACTION_DOWN)
                    drowBackgroud(true);
            } else {
                drowBackgroud(false);
            }
            return true;

        }else{
            return super.onTouchEvent(event);
        }
    }
    /**
     * 设置按下颜色值
     */
    private void drowBackgroud(boolean isTouch) {
        if (isTouch) {
            if(solidTouchColor!=0)
                paint.setColor(solidTouchColor);
            if(paintStroke!=null)
                paintStroke.setColor(strokeTouchColor);
            if(textTouchColor!=0)
                setTextColor(textTouchColor);
        } else {
            if(solidColor!=0)
                paint.setColor(solidColor);

            if(paintStroke!=null)
                paintStroke.setColor(strokeColor);

            if(textTouchColor!=0)
                setTextColor(textColor);
        }


        postInvalidate();
    }

}
